import addDialogHtml from 'ngtemplate!html!../../../tpls/user/add.dialog.tpl.html';
import setMaterialHtml from 'ngtemplate!html!../../../tpls/user/setmaterial.dialog.tpl.html';

class MaterialController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, UserService, iRunner, getCompanyList){

        var vm = this;

        vm.$rootScope     = $rootScope;
        vm.$cookieStore   = $cookieStore;
        vm.$timeout       = $timeout;
        vm.$state         = $state;
        vm.ngDialog       = ngDialog;
        vm.dialogService  = dialogService;
        vm.UserService    = UserService;
        vm.iRunner        = iRunner;
        vm.getCompanyList = getCompanyList;

        console.log(vm.getCompanyList.data);


        // 返回计时设备list
        // 连接路由 http://timing.com/dashboard/material/lists返回数据
        vm.UserService.getMaterialLists(vm.$cookieStore.get('access_token'))
        .then(function(data) {
            // 调用成功的场合
            if (data.code == 0) {
                vm.materialLists = data.data;
            } else {
                vm.dialogService.error(data.message)
            }
            // 登录信息已过期, 请重新登录
            data.code==-1001&&vm.dialogService.error(data.message, true);
            
        }, function(data) {
            console.log('error')
        });

        /**
         * [add 添加设备弹窗]
         */
        vm.add = function() {

            // $rootScope.companyNameList = [];
            // angular.forEach(vm.getCompanyList.data, function(data,index,array){
            //     if (data.status == "normal") {            
            //         $rootScope.companyNameList.push(data.company_name);
            //     }
            // });

            vm.ngDialog.open({
                template: addDialogHtml,
                className: 'ngdialog-theme-default ngdialog-alert',
                scope: $rootScope
            });
        }

        /**
         * [addSave 添加设备保存]
         * @param {[type]} company_name      [公司名称]
         * @param {[type]} material_name     [设备名称]
         * @param {[type]} material_title    [设备别名]
         * @param {[type]} material_status   [可用状态]
         * @param {[type]} material_type     [类型]
         * @param {[type]} material_rentable [可否租借]
         */
        $rootScope.addSave = function(material_name, material_title, material_status, material_type, material_rentable) {
            // 未填写完全设备信息的场合
            if (!material_name || !material_title || !material_status || !material_type || !material_rentable) {
                vm.dialogService.error('请填写完整设备信息！！！');
            }

            // 验证通过的场合
            // 添加参数
            var params = {
                name: material_name,
                title: material_title,
                status: material_status,
                type: material_type,
                is_rentable: material_rentable
            }
            // 调用接口
            vm.UserService.postMaterialAdd(params, vm.$cookieStore.get('access_token'))
            .then(function(data) {
                // 调用成功的场合
                if (data.code == 0) {
                    vm.dialogService.success('设备 '+ material_name + ' 添加成功');
                } else {
                    vm.dialogService.error(data.message)
                }
                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                console.log(data)
            }, function(data) {
                console.log('error')
            });

        }

        /**
         * [unbind 解绑设备]
         * @param  {[type]} id [description]
         * @return {[type]}    [description]
         */
        vm.unbind = function(name, mid) {
            vm.UserService.getUnbind(vm.$cookieStore.get('access_token'), name + '&mid=' + mid)
            .then(function(data) {
                // 调用成功的场合
                if (data.code == 0) {
                    vm.dialogService.success('设备 '+ name + ' 解绑成功');
                } else {
                    vm.dialogService.error(data.message)
                }
                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                console.log(data)
            }, function(data) {
                console.log('error')
            });
        }

        /**
         * [setMaterial 分配设备]
         */
        vm.setMaterial = function(name, mid) {
            $rootScope.companyList = vm.getCompanyList.data;
            vm.ngDialog.open({
                template: setMaterialHtml,
                className: 'ngdialog-theme-default ngdialog-alert',
                scope: $rootScope
            });


            $rootScope.setSave = function(company_id_material) {

                if (!company_id_material) {
                    vm.dialogService.error('请选择要分配的机构');
                    return false;
                }
                var params = {
                    company_id: company_id_material,
                    name: name,
                    mid: mid
                }
                // 调用接口分配设备给机构
                vm.UserService.postDistributeMaterial(params, vm.$cookieStore.get('access_token'))
                .then(function(data) {
                    // 调用成功的场合
                    if (data.code == 0) {
                        vm.dialogService.success('分配成功');
                    } else {
                        vm.dialogService.error(data.message)
                    }
                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);
                    console.log(data)
                }, function(data) {
                    console.log('error')
                });


            }
        }
    }
}

MaterialController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'UserService', 'iRunner', 'getCompanyList']; 

export default MaterialController;